<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['projects:SlCustom:remove']"
        >批量禁用</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['projects:SlCustom:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="SlCustomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="性别" align="center" prop="gender" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
          </template>
        </el-table-column>
      <el-table-column label="头像" align="center" >
        <template slot-scope="scope">
         <img :src="scope.row.avatarurl" alt="图片描述" width="50" height="50" />
       </template>
      </el-table-column>
      <el-table-column label="总积分" align="center" prop="integral" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="是否禁用" align="center" prop="ifFlag" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_flog" :value="scope.row.ifFlag"/>
          </template>
        </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleCustomInfo(scope.row)"
            v-hasPermi="['projects:SlCustom:add']"
          >客户资料</el-button>
          <el-button
            size="mini"
            type="text"
            :icon="Number(scope.row.ifFlag) === 0 ? 'el-icon-delete' : 'el-icon-check'"
            @click="Number(scope.row.ifFlag) === 0 ? handleDelete(scope.row) : handleEnable(scope.row)"
            v-hasPermi="['projects:SlCustom:remove']"
            :disabled="false"  
          >
            {{ Number(scope.row.ifFlag) === 0 ? '禁用' : '启用' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

<!-- 完善客户资料 -->
<el-dialog :title="title" :visible.sync="openInfo" width="500px" append-to-body>
      <el-form ref="infoForm" :model="infoForm" :rules="infoFormRules" label-width="80px">
        <el-form-item label="编号" prop="infoNo">
          <el-input v-model="infoForm.infoNo" type="number" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="称呼" prop="realName">
          <el-input v-model="infoForm.realName" placeholder="请输入称呼" />
        </el-form-item>
        <el-form-item label="联系方式" prop="tel">
          <el-input v-model="infoForm.tel" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="居住地址" prop="address">
          <el-input v-model="infoForm.address" placeholder="请输入居住地址" />
        </el-form-item>
        <el-form-item label="职业" prop="work">
          <el-input v-model="infoForm.work" placeholder="请输入职业" />
        </el-form-item>
        <el-form-item label="交通方式" prop="traffic">
          <el-input v-model="infoForm.traffic" placeholder="请输入交通方式" />
        </el-form-item>
        <el-form-item label="了解途径" prop="waysCome">
          <el-select v-model="infoForm.waysCome" placeholder="请选择了解途径">
            <el-option
              v-for="dict in dict.type.ways_come_types"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="脱毛部位" prop="removalParts">
          <el-input  type = "textarea" v-model="infoForm.removalParts" placeholder="请输入脱毛部位" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input  type = "textarea" v-model="infoForm.remark" placeholder="请输入备注注意信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitInfoForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>


</template>

<script>
import { listSlCustom, getSlCustom, delSlCustom, addSlCustom, updateSlCustom } from "@/api/projects/SlCustom";
import { getSlCustomInfoByUserId, addSlCustomInfo, updateSlCustomInfo } from "@/api/projects/SlCustomInfo";
export default {
  name: "SlCustom", 
  dicts: ['sys_user_sex','user_flog','ways_come_types'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 森理会员表格数据
      SlCustomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openId: null,
        nickname: null,
        phone: null,
      },
      // 表单参数
      form: {},
      infoForm: {},
      // 表单校验
      infoFormRules: {
        infoNo: [{ required: true, message: '请输入编号', trigger: 'blur' }],
        realName: [{ required: true, message: '请输入称呼', trigger: 'blur' }],
        tel: [{ required: true, message: '请输入联系方式', trigger: 'blur' }],
        removalParts: [{ required: true, message: '请输入脱毛部位', trigger: 'blur' }],
        remark: [{ required: true, message: '请输入备注注意信息', trigger: 'blur' }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询森理会员列表 */
    getList() {
      this.loading = true;
      listSlCustom(this.queryParams).then(response => {
        this.SlCustomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        openId: null,
        nickname: null,
        phone: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认禁用森理会员编号为"' + ids + '"的数据项？').then(function() {
        return delSlCustom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("禁用成功");
      }).catch(() => {});
    },
    handleEnable(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认启用森理会员编号为"' + ids + '"的数据项？').then(function() {
        return delSlCustom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("启用成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('projects/SlCustom/export', {
        ...this.queryParams
      }, `SlCustom_${new Date().getTime()}.xlsx`)
    },

    /*************************客户资料************************************* */
    /** 查看客户资料弹框按钮操作 */
    handleCustomInfo(row) {
     getSlCustomInfoByUserId(row.id).then(response => {
        if(response.data){
          this.infoForm = response.data;
        }else{
          this.infoForm.userId = row.userId;
        }
      })
      this.reset();
      this.openInfo = true;
      this.title = "客户资料";
    },
    submitInfoForm() {
      this.$refs.infoForm.validate(valid => {
        if (valid) {
          if(this.infoForm.userId){
            updateSlCustomInfo(this.infoForm).then(response => {
              this.$modal.msgSuccess("更新成功");
              this.getList();
              this.cancel();
            });
          }else{
            addSlCustomInfo(this.infoForm).then(response => {
              this.$modal.msgSuccess("资料添加成功");
              this.getList();
              this.cancel();
            });
          }
        }
      });
    }

  }
};
</script>
